﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using CSharp.Libs.Koc;
using System.Windows;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace CSharp.Libs.WPF.UserControls
{
    /// <summary>
    /// 
    /// </summary>
    /// <history>created  06.06.2012</history>
    class PostHandlerControlViewModel : ViewModelBase
    {
        #region declarations
        private Dispatcher _DISP = null;
        private PostHandler _PH = null;

        private ObservableCollection<PostHandlerItem> _ocPostItems = new ObservableCollection<PostHandlerItem>();

        private RelayCommand _rcStartPostHandling=null;
        private RelayCommand _rcStoppPostHandling=null;
        #endregion



        #region constructor / finalizer
        public PostHandlerControlViewModel(
            Dispatcher DISP)
        {
            _DISP = DISP;
        }
        #endregion



        #region event handling
        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        void _PH_TimerStarted(object sender)
        {
            _changePostHandlerActive();
            _updateList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        void _PH_TimerStopped(object sender)
        {
            _changePostHandlerActive();
            _updateList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <history>created  08.06.2012</history>
        void _PH_ItemsChanged(object sender)
        {
            _updateList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="psea"></param>
        /// <history>created  09.06.2012</history>
        void _PH_PostFailed(object sender, PostEventArgs psea)
        {
            _updateList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="psea"></param>
        /// <history>created  09.06.2012</history>
        void _PH_PostSuccessful(object sender, PostEventArgs psea)
        {
            _updateList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <history>created  09.06.2012</history>
        void _PH_TimerElapsed(object sender)
        {
            _updateList();
        }
        #endregion



        #region private functions
        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        private void _changePostHandlerActive()
        {
            _DISP.Invoke((Action)delegate
            {
                OnPropertyChanged("PostHandlerActive");
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  08.06.2012</history>
        private void _startPostHandling()
        {
            if (_PH != null)
                _PH.StartPosting();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  08.06.2012</history>
        private void _stoppPostHandling()
        {
            _PH.StopPosting();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  09.10.2012</history>
        private void _updateList()
        {
            _DISP.Invoke((Action)delegate
            {
                _ocPostItems.Clear();

                foreach (PostHandlerItem PHI in _PH.PostItems)
                {
                    _ocPostItems.Add(PHI);
                }

                OnPropertyChanged("PostHandlerItems");
            });
        }
        #endregion


        

        #region interface
        #region properties
        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        public Visibility PostHandlerActive
        {
            get
            {
                if (_PH != null)
                {
                    if (_PH.PostingActive) { return Visibility.Visible; }
                    else { return Visibility.Hidden; }
                }
                else { return Visibility.Hidden; }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        public ObservableCollection<PostHandlerItem> PostHandlerItems { get { return _ocPostItems; } }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  08.06.2012</history>
        public ICommand StartPostHandling
        {
            get
            {
                if (_rcStartPostHandling == null)
                {
                    _rcStartPostHandling = new RelayCommand(
                        param => this._startPostHandling(),
                        param => this.CanStartPostHandling);
                }
                return _rcStartPostHandling;
            }
        }
        public bool CanStartPostHandling { get { return true; } }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  08.06.2012</history>
        public ICommand StoppPostHandling
        {
            get
            {
                if (_rcStoppPostHandling == null)
                {
                    _rcStoppPostHandling = new RelayCommand(
                        param => this._stoppPostHandling(),
                        param => this.CanStoppPostHandling);
                }
                return _rcStoppPostHandling;
            }
        }
        public bool CanStoppPostHandling { get { return true; } }
        #endregion

        #region methods
        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        public void InitViewModel(
            PostHandler PH)
        {
            _PH = PH;

            _PH.TimerStarted += new PostHandler.TimerStartedEventHandler(_PH_TimerStarted);
            _PH.TimerStopped += new PostHandler.TimerStoppedEventHandler(_PH_TimerStopped);
            _PH.ItemsChanged += new PostHandler.ItemsChangedEventHandler(_PH_ItemsChanged);
            _PH.PostSuccessful += new PostHandler.PostSuccessfulEventHandler(_PH_PostSuccessful);
            _PH.PostFailed += new PostHandler.PostFailedEventHandler(_PH_PostFailed);
            _PH.TimerElapsed += new PostHandler.TimerElapsedEventHandler(_PH_TimerElapsed);
        }
        #endregion
        #endregion
    }
}
